<?php

namespace cainiao;

//use catcher\base\CatchRequest as Request;
use cainiao\api\v1\Link;
use catchAdmin\customs\model\CnOrder;
use houniao\api\v1\Goods;
use think\facade\Db;
use think\Request;
use catcher\CatchResponse;
use houniao\tools\Params;
use think\Exception;
use think\facade\Log;

class Notify
{

    public function notify(Request $request){
        $data = $request->param();

//        $data = '{"logistics_interface":"{\"externalShopName\":\"\u56fd\u9645\u65e5\u7528\u54c1\u5546\u57ce\",\"receiverInfo\":{\"address\":\"\u6c5f\u82cf\u7701^^^*****\",\"province\":\"\u6c5f\u82cf\u7701\",\"city\":\"\u5357\u4eac\u5e02\",\"district\":\"\u96e8\u82b1\u53f0\u533a\",\"name\":\"\u5f90**\",\"contactNo\":\"151****8887\"},\"ownerUserId\":\"2206903100025\",\"externalShopId\":\"\",\"orderCode\":\"12323488813123334581251\",\"orderConfirmTime\":\"2023-07-13 13:59:17\",\"lgOrderCode\":\"LP00587430422142\",\"tmsOrders\":[{\"tmsCode\":\"ZTO\",\"tmsOrderCode\":\"78704438802805\",\"tmsItems\":[{\"itemId\":\"610240611644\",\"quantity\":\"1\",\"itemCode\":\"new_inventory_item_b\"},{\"itemId\":\"641295234569\",\"quantity\":\"1\",\"itemCode\":\"324256567\"},{\"itemId\":\"643336351345\",\"quantity\":\"1\",\"itemCode\":\"34557687\"}],\"packageHeight\":\"300\",\"packageWeight\":\"1325\",\"packageLength\":\"100\",\"packageWidth\":\"200\"}],\"confirmType\":\"0\",\"orderItems\":[{\"itemId\":\"610240611644\",\"quantity\":\"1\",\"itemCode\":\"new_inventory_item_b\",\"orderSourceCode\":\"12323488813123334581251\",\"ownUserId\":\"2206903100025\",\"subSourceCode\":\"12323488813123334581251\",\"extItemId\":\"610240611644\",\"extendField\":{}},{\"itemId\":\"641295234569\",\"quantity\":\"1\",\"itemCode\":\"324256567\",\"orderSourceCode\":\"12323488813123334581251\",\"ownUserId\":\"2206903100025\",\"subSourceCode\":\"12323488813123334581251\",\"extItemId\":\"641295234569\",\"extendField\":{}},{\"itemId\":\"643336351345\",\"quantity\":\"1\",\"itemCode\":\"34557687\",\"orderSourceCode\":\"12323488813123334581251\",\"ownUserId\":\"2206903100025\",\"subSourceCode\":\"12323488813123334581251\",\"extItemId\":\"643336351345\",\"extendField\":{}}],\"storeCode\":\"BSZXCCS001\"}","data_digest":"2wpkDpcxXTE2nTMqbAPjJw==","partner_code":"7ac4726f88b4da929660d92fa90ca881","from_code":"cnimf","msg_type":"GLOBAL_SALE_ORDER_CONFIRM","msg_id":"LP00587430422142"}';
//        $data = json_decode($data,true);

        event('cainiaoNotify',$data);

//        Log::write("-------菜鸟回调数据start------");
//        Log::write(json_encode($data));
//        Log::write("-------菜鸟回调数据end------");


        try {
            if($data['msg_type'] == 'GLOBAL_SALE_ORDER_CONFIRM'){//订单出库及包裹回传
                    $order = (new CnOrder)->where('cnOrderCode',$data['msg_id'])->find();
                    $order->push_status = "已出库";
                    $order->packageInfo =  $data['logistics_interface'] ?? json_encode([]);
                    $order->save();
            }
//            if($data['msg_type'] == 'GLOBAL_SALE_ORDER_STATUS_SYNC'){//订单状态变化
//                $cainiaoApi = new Link;
//                $logistics_interface = json_decode($data['logistics_interface'],true);
//
//                $requestData = [
//                    'userId'=>$logistics_interface['ownerUserId'],
//                    'lgOrderCode'=>$logistics_interface['lgOrderCode']
//                ];
//
//                $result =  $cainiaoApi->CROSSBORDER_LOGISTICS_DETAIL_QUERY($requestData);
//
//                if($result['success'] == 'true'){//成功
//                    $order = (new CnOrder)->where('cnOrderCode',$data['msg_id'])->find();
//                    $order->push_status = $result['nodeList'][0]['packageStatus']['logisticStatusDesc'];
//                    $order->mailNo = $result['nodeList'][0]['mailNo'];
//                    $order->partnerCode = $result['nodeList'][0]['partnerList'][0]['partnerCode'];
//                    $order->partnerName = $result['nodeList'][0]['partnerList'][0]['partnerName'];
//                    $order->save();
//                }else{
//                    die(json_encode([
//                        'success'=> false,
//                        'errorMsg'=>$result['errorMsg']
//                    ]));
//                }
//            }
        } catch (\Exception $e) {
            die(json_encode([
                'success'=> false,
                'errorMsg'=>$e->getMessage()
            ]));
        }

        die(json_encode(['success'=> true]));
    }

    //检验sign
    public function checkSign($data,$request_sign)
    {
        $headContent = [
            'head'=>$data['head'],
            'content'=>$data['content']
        ];

//        dd($headcontent);

        $headcontent1 = md5(stripslashes(json_encode($headContent,JSON_UNESCAPED_UNICODE)));

//        $private_key = $this->private_key;
//        $private_key = chunk_split($private_key,64,"\n");
//        $private_key = "-----BEGIN RSA PRIVATE KEY-----\n{$private_key}-----END RSA PRIVATE KEY-----\n";
//        openssl_sign( $headcontent1 ,$encry, $private_key,OPENSSL_ALGO_SHA256);
//        dd(base64_encode($encry));

        $makeSign = md5($headcontent1 . $this->public_key);

        if ($makeSign == $request_sign) {
            return true;
        } else {
            return false;
        }
    }


}